package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.im.immodel.IMUtils;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.CallPhoneEntity;
import com.sgb.kjwl.view.ui.adapter.material.CallPhoneAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.List;

public class CallPhonePopWindow extends BasePopWindow<String> {
    private MaxHeightRecyclerView netAlbumRecycler;
    private CallPhoneAdapter adapter;
    private TextView tvCancel;

    public CallPhonePopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_call_phone;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        netAlbumRecycler = holder.getPop().findViewById(R.id.net_album_recycler);
        tvCancel = holder.getPop().findViewById(R.id.tv_cancel);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        adapter = new CallPhoneAdapter(mContext, null);
        netAlbumRecycler.setLayoutManager(linearLayoutManager);
        netAlbumRecycler.setAdapter(adapter);
        adapter.setOnItemClick(new AdapterOnItemClick<CallPhoneEntity>() {
            @Override
            public void onItemClick(CallPhoneEntity albumEntity, int position) {
                if("0".equals(albumEntity.getType())){
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    Uri data = Uri.parse("tel:" + albumEntity.getPhone());
                    intent.setData(data);
                    mContext.startActivity(intent);
                }else{
                    if (!TextUtils.isEmpty(String.valueOf(albumEntity.getImAccount()))) {
                        IMUtils.getInstance().buildSingleTalking(String.valueOf(albumEntity.getImAccount()));
                    }
                }
                dismiss();
            }
        });
        tvCancel.setOnClickListener(v -> {
            dismiss();
        });
    }

    public void setData(List<CallPhoneEntity> datas) {
        if (datas.size() < 0) {
            return;
        }
        adapter.setDatas(datas);
    }
}
